{
  LW VPObject -- -- LightWave Viewport Object

  A variant parameter, or vparm, is a double-precision variable or 3-vector
  that can vary as a function of time. Vparms are used as containers for
  the values of XPanel controls that can be enveloped or textured
  (any control with "-env" in its type name).
  That's the rationale for the existence of vparms,
  but you're free to use them for other things as well.

  Based on lwvpobject.h which is:
  Copyright 2010, NewTek, Inc.
}

{
  Copyright 2012, Jameel Halabi

  This unit is part of the BlaiseWave wrapper,
  which is licensed under the BSD 2-Clause license (see licenses/license.txt).
}

unit LW_VPObject;

{$mode objfpc}{$H+}

interface

uses
  LW_Types   ,
  LW_Server  ,
  // LW_Generic ,
  LW_Handler ,
  LW_Render  ;

const
  { Viewport object window handler. }
  LWVIEWPORTOBJECT_HCLASS  = 'ViewportObjectHandler'   ;
  LWVIEWPORTOBJECT_ICLASS  = 'ViewportObjectInterface' ;
  LWVIEWPORTOBJECT_VERSION = 3 ;

  { Rendering Flags. }
  // enum en_lwviewflags
  LWVF_a      = 0 ; // Buffer has Alpha in it.
  LWVF_z      = 1 ; // Z Buffer is valid.
  LWVF_sizeof = 2 ;

  { Viewport object camera global. }
  LWVIEWPORTCAMERA = 'LW Viewport Camera' ;

  // enum  LWViewportObjectCameraEnabled
  LWVPOC_Disabled             = 0 ;
  LWVPOC_Enabled_Orthographic = 1 ;
  LWVPOC_Enabled_Perspective  = 2 ;

  { Menu Flags. }
  // enum LWViewportFlags
  LWVPF_openglWireframe = 0 ;  // Displays wireframe over viewport object.
  LWVPF_openglOverlay   = 1 ;  // Displays OpenGL overlays.
  LWVPF_sizeof          = 2;

  { Viewport object menu global. }
  LWVIEWPORTMENU  = 'LW Viewport Menu' ;



type

  PLWVPAHostAccess = ^TLWVPAHostAccess ;
  TLWVPAHostAccess = record end        ;

  PLWViewportObjectAccess = ^TLWViewportObjectAccess ;
  TLWViewportObjectAccess = record
    ShowFrame   : function
                  (       HostAccess     : PLWVPAHostAccess ;
                          Width          ,
                          Height         : UInt32           ;
                          RGBBuffer      : PSingle          ;
                          ModulusRGB     : UInt32           ;
                          ZBuffer        : PSingle          ;
                          ModulusZ       ,
                          Scale          : UInt32           ;
                          FrameComplete  ,
                          Redraw         ,
                          EyeToRender    : Integer          ) : Integer ;
  end;

  PLWInputHandling = ^TLWInputHandling ;
  TLWInputHandling = record end        ;

  PLWViewportHandler = ^TLWViewportHandler;
  TLWViewportHandler = record
    Inst        : PLWInstanceFuncs;
    Item        : PLWItemFuncs    ;

    NewTime       : function
                    (       Instance  : PLWInstance    ;
                            Time      : Double         ) : Integer ;  // Time.
    NewSize       : function
                    (       Instance  : PLWInstance    ;
                            Width     ,
                            Height    : UInt32         ) : Integer ;  // Width and Height.
    Redraw        : function
                    (       Instance  : PLWInstance    ) : Integer ;  // Redraws the window.
    SetViewIndex  : function
                    (       Instance  : PLWInstance    ;
                            ViewIndex : Uint32         ) : Integer ;  // View 0 to 3.
    SetViewMode   : function
                    (       Instance  : PLWInstance    ;
                            ViewMode  : Uint32         ) : Integer ;  // LVVIEWT.
    SetAccess     : function
                    (       Instance  : PLWInstance    ;
                            Access    : PLWViewportObjectAccess ;
                            Host      : PLWVPAHostAccess ) : Integer ;
    Flags         : function
                    (       Instance  : PLWInstance     ) : UInt32  ; // LWViewFlags.
    Start         : function
                    (       Instance  : PLWInstance     ;
                            SubDLevel : UInt32          ) : Integer ; // Start rendering.
    Stop          : function
                    (       Instance  : PLWInstance     ) : Integer ; // Done, free memory.
    Selected      : function
                    (       Instance  : PLWInstance     ;
                            Select    : Integer         ) : PLWError ; // Currently selected or unselected.
    Cleanup       : function
                    (       Instance  : PLWInstance     ) : Integer  ; // Cleanup after redraw.

    { Layout pull down menu. }

    MenuCount     : function
                    (       Instance  : PLWInstance     ) : Integer ; // Count of entries in the menu.
    MenuName      : function
                    (       Instance  : PLWInstance     ;
                            MenuEntry : Uint32          ) : Pchar   ; // Name  of menu entry. // const
    MenuGetPick   : function
                    (       Instance  : PLWInstance     ;
                            MenuEntry : Uint32          ) : Integer ; // Menu set entry selected.
    MenuSetPick   : function
                    (       Instance  : PLWInstance     ;
                            MenuEntry : Uint32          ;
                            Selected  : Integer         ) : Integer ; // Menu get entry selected.

    InputHandling : procedure
                    (       Instance  : PLWInstance     ;
                            Handler   : PLWInputHandling) ;           // Returns the input handler
  end;

  PLWViewportObjectCamera = ^TLWViewportObjectCamera ;
  TLWViewportObjectCamera = record
    FilmSize                 ,                // Film size for viewport.
    FocalLength              ,                // Focal length of viewport.
    Aspect                   ,                // Pixel aspect ratio of viewport.
    ZNear                    ,                // z Near clipping plane.
    ZFar                     :  Double      ; // z Far  clipping plane.
    ModelMatrix              ,                // OpenGL model view matrix for viewport.
    ModelMatrixInverted      ,                // Inverse of the model view matrix.
    ProjectionMatrix         ,                // OpenGL projection matrix for viewport.
    ProjectionMatrixInverted ,                // Inverse of the projection matrix.
    ViewMatrix               ,                // Combined model and projection view matrix.
    ViewMatrixInverted       :  TLWDMatrix4 ; // Inverse of the view matrix.
    ColorBackground          :  array [0..3] of Single ; // Back ground clear color.
    GetRay                   :  function
                                (      VPOCamera : PLWViewportObjectCamera ;
                                       FX        ,
    			               FY        : Double                  ;
                                       Position  ,
                                       Direction : Double                  ) : Integer ; // Ray tracing position and direction.
    Enabled                  :  Integer    ; // >0 for supported viewport camera modes.
    Origin                   :  TLWDVector ; // Camera origin in world coordinates.
    GetOrigin                :  function
                                (      VPOCamera   : PLWViewportObjectCamera ;
                                       CamWSOrigin : PDouble                 ) : Integer ; // Camera world space origin.
  end;

  { LightWave function global proto type.
    The global returns this function pointer. }
  TLWViewportCameraFunc = function
                          (       ViewIndex   ,
                                  ViewMode    : Uint32  ;
                                  EyeToRender : Integer ) : PLWViewportObjectCamera ;


  PLWViewportObjectMenu = ^TLWViewportObjectMenu ;
  TLWViewportObjectMenu = record
    GetFlags : function
               (       ViewIndex   : Uint32  ) : UInt32  ; // View 0 to 3.
    SetFlags : function
               (       ViewIndex   ,
                       ViewFlags   : Uint32  ) : Integer ; // View 0 to 3, LWViewportFlags.
  end ;

implementation

end.

